home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Scope / Scope Disk #060 (199x)(Scope PD)(US)[WB].zip / Scope Disk #060 (199x)(Scope PD)(US)[WB].adf / Wrap / wrap.c < prev    next >
C/C++ Source or Header  |  1988-11-06  |  8KB  |  339 lines

  1. /* November 6th, 1988 */
  2.  
  3. /* I took the MachMouse source code and cut it down to just the parts  */
  4. /* which were needed for the pointer manipulation, So this source is   */
  5. /* actually already in the PD. Anyway, I just thought I'd include it   */
  6. /* because I HATE IT WHEN SOMEONE DOESN'T DISTRIBUTE THEIR SOURCE CODE */
  7. /* WITH THEIR PROGRAM. drives me nuts. This is totally PD, ofcourse.   */
  8. /* Probably not perfectly written, and no documentation - sorry. */
  9. /* Used Manx C v3.4b */
  10.  
  11. /* Enjoy, K.Mardam-Bey */
  12.  
  13. #include <devices/input.h>
  14. #include <devices/inputevent.h>
  15. #include <devices/timer.h>
  16. #include <exec/types.h>
  17. #include <exec/interrupts.h>
  18. #include <exec/memory.h>
  19. #include <exec/ports.h>
  20. #include <functions.h>
  21. #include <graphics/gfxmacros.h>
  22. #include <intuition/intuitionbase.h>
  23.  
  24. #define QUIT         0x1
  25. #define MOVEIT       0x4
  26. #define DELAY    1000000L
  27. #define LAMIGA       0x66
  28. #define STOPIT       0x41
  29.  
  30. char PortName[] = "PPT";
  31.  
  32. short left, right;
  33. short dx,dy;
  34. short x,y;
  35. short event;
  36. short lastcode = 0;
  37. long  sec = 0,micro = 0;
  38. short rbuttonisdown = 0, lbuttonisdown = 0;
  39.  
  40. struct MsgPort   *inputPort = NULL;
  41. struct IOStdReq  *inputReq = NULL;
  42. struct MsgPort   *TimerPort = NULL;
  43. struct Screen    *s;
  44. struct IntuitionBase *IntuitionBase = NULL;
  45.  
  46. struct timerequest Timer_Req;
  47. long   TimerSig,tdevice = 1;
  48.  
  49. struct InputEvent phoney;
  50. struct HotInfo
  51.   {
  52.   struct Task *hotTask;
  53.   long  hotSig;
  54.   } hotStuff;
  55.  
  56. long signum = -1;
  57.  
  58. struct Interrupt handlerStuff;
  59. struct defPort
  60. {
  61.   struct MsgPort mp;
  62. };
  63.  
  64. struct defPort *defPortPtr;
  65. char defPortName[] = "MaDP";
  66. short updating = 0;
  67.  
  68. void HandlerInterface()
  69. {
  70. #asm
  71.   movem.l a4,-(sp)
  72.   jsr _geta4#
  73.   movem.l   A0/A1,-(sp)
  74.   jsr       _myhandler
  75.   addq.l    #8,A7
  76.   movem.l (sp)+,a4
  77. #endasm
  78. }
  79.  
  80. struct InputEvent *myhandler(ev1, hotStuff)
  81. struct InputEvent *ev1;
  82. struct HotInfo *hotStuff;
  83. {
  84.   struct InputEvent *ev, *last;
  85.   short removeit;
  86.   short evcode,evqual;
  87.  
  88.   event = 0;
  89.   for (ev=ev1,last = NULL; ev; ev=ev->ie_NextEvent)
  90.   {
  91.     evcode = ev->ie_Code;
  92.     evqual = ev->ie_Qualifier;
  93.     removeit = 0;
  94.  
  95.     if ((ev->ie_Class != IECLASS_TIMER))
  96.     {
  97.       if (ev->ie_Class == IECLASS_RAWKEY)
  98.       {
  99.         if ((evcode >= 0x80) && (evcode == (lastcode | 0x80))) {
  100.           removeit = 1;
  101.           lastcode = 0; }
  102.         else
  103.           if (evcode == LAMIGA) {
  104.             lastcode = evcode;
  105.             removeit = 1; }
  106.         else {
  107.           lastcode = 0;
  108.           removeit = 0;
  109.         }
  110.  
  111.         if ((evcode == STOPIT)&&((evqual & IEQUALIFIER_LCOMMAND) == IEQUALIFIER_LCOMMAND))
  112.         {
  113.           lastcode = evcode;
  114.           removeit = 1;
  115.           event |= QUIT;
  116.         }
  117.       }
  118.     }
  119.  
  120.     if (ev->ie_Class == IECLASS_RAWMOUSE)
  121.     {
  122.       if (!left && (evcode == IECODE_LBUTTON))
  123.         lbuttonisdown = 1;
  124.       if (!right && (evcode == IECODE_RBUTTON))
  125.         rbuttonisdown = 1;
  126.  
  127.       s = IntuitionBase->FirstScreen;
  128.       while (s && (s->MouseY < 0)) s = s->NextScreen;
  129.       if (s == NULL) s = IntuitionBase->ActiveScreen;
  130.  
  131.       x = s->MouseX + ev->ie_X;
  132.       y = s->MouseY + ev->ie_Y;
  133.  
  134.       if ((ev->ie_TimeStamp.tv_secs == sec) && ((ev->ie_TimeStamp.tv_micro / 50000) == micro))
  135.       {
  136.         dx = ev->ie_X;
  137.         dy = ev->ie_Y;
  138.         if (!lbuttonisdown && !rbuttonisdown) event |= MOVEIT;
  139.       }
  140.  
  141.       if (ev->ie_TimeStamp.tv_secs)
  142.       {
  143.         micro = ev->ie_TimeStamp.tv_micro / 50000;
  144.         sec = ev->ie_TimeStamp.tv_secs;
  145.       }
  146.  
  147.       if (evcode == (IECODE_LBUTTON | 0x80))
  148.         lbuttonisdown = 0;
  149.       if (evcode == (IECODE_RBUTTON | 0x80))
  150.         rbuttonisdown = 0;
  151.     }
  152.  
  153.     if (removeit)
  154.       if (last == NULL)
  155.         ev1 = ev->ie_NextEvent;
  156.       else
  157.         last->ie_NextEvent = ev->ie_NextEvent;
  158.     else
  159.       last = ev;
  160.     }
  161.  
  162.   if (event)
  163.     Signal(hotStuff->hotTask,hotStuff->hotSig);
  164.   return(ev1);
  165. }
  166.  
  167. main (argc, argv)
  168. int argc;
  169. char *argv[];
  170. {
  171.   struct IntuiMessage *Msg;
  172.   long                class;
  173.   long                len;
  174.   short               i,j,f,c;
  175.  
  176.   IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0L);
  177.  
  178.   defPortPtr = (struct defPort *) FindPort(defPortName);
  179.   if (defPortPtr == NULL)
  180.   {
  181.     if ((defPortPtr = (struct defPort *) AllocMem((long)sizeof(struct defPort),MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  182.       exit(10);
  183.   }
  184.   else
  185.     updating = 1;
  186.  
  187.   if (updating)
  188.     Uninstall();
  189.  
  190.   i = left = right = 0;
  191.  
  192.   while (argv[1][i]) {
  193.     if (argv[1][i] == 'l')
  194.       left = 1;
  195.     if (argv[1][i] == 'r')
  196.       right = 1;
  197.     i += 1;
  198.   }
  199.  
  200.   defPortPtr->mp.mp_Node.ln_Pri = 0;
  201.   defPortPtr->mp.mp_Node.ln_Type = NT_MSGPORT;
  202.   NewList(&(defPortPtr->mp.mp_MsgList));
  203.   defPortPtr->mp.mp_Node.ln_Name = (char *) &(defPortName);
  204.  
  205.   AddPort(defPortPtr);
  206.  
  207.   if((signum = AllocSignal((long)-1)) == -1)
  208.     Uninstall();
  209.  
  210.   hotStuff.hotSig = 1 << signum;
  211.   hotStuff.hotTask = FindTask(NULL);
  212.  
  213.   if(!(inputPort = CreatePort(PortName,0)))
  214.     Uninstall();
  215.   if(!(inputReq = CreateStdIO(inputPort)))
  216.     Uninstall();
  217.  
  218.   handlerStuff.is_Data = (APTR)&hotStuff;
  219.   handlerStuff.is_Code = HandlerInterface;
  220.   handlerStuff.is_Node.ln_Pri = 55;
  221.   handlerStuff.is_Node.ln_Name = "PopHan";
  222.  
  223.   if(OpenDevice("input.device",0L,inputReq,0L) != 0)
  224.     Uninstall();
  225.  
  226.   inputReq->io_Command = IND_ADDHANDLER;
  227.   inputReq->io_Data = (APTR)&handlerStuff;
  228.  
  229.   DoIO(inputReq);
  230.  
  231.   if ((TimerPort = CreatePort("TimP", 0L)) == NULL)
  232.     Uninstall();
  233.  
  234.   if ((tdevice = OpenDevice(TIMERNAME, UNIT_VBLANK, &Timer_Req, 0L)) != 0)
  235.     Uninstall();
  236.   Timer_Req.tr_node.io_Message.mn_ReplyPort = TimerPort;
  237.   Timer_Req.tr_node.io_Command = TR_ADDREQUEST;
  238.   Timer_Req.tr_node.io_Flags = 0;
  239.   Timer_Req.tr_node.io_Error = 0;
  240.  
  241.   TimerSig = (1L << TimerPort->mp_SigBit);
  242.  
  243.   (void)SetTaskPri(FindTask(NULL), 0L);
  244.  
  245.   QueTimer();
  246.  
  247.   for (;;)
  248.   {
  249.     Wait(hotStuff.hotSig | TimerSig);
  250.  
  251.     if (Msg = (struct IntuiMessage *)GetMsg(TimerPort))
  252.       if (Msg) QueTimer();
  253.  
  254.     if (event & MOVEIT)
  255.       MovePointer();
  256.  
  257.     if (event & QUIT)
  258.       Uninstall();
  259.   }
  260. }
  261.  
  262. QueTimer()
  263. {
  264.   Timer_Req.tr_time.tv_secs = 0;
  265.   Timer_Req.tr_time.tv_micro = DELAY;
  266.   SendIO(&Timer_Req.tr_node);
  267. }
  268.  
  269. MovePointer()
  270. {
  271.   s = IntuitionBase->ActiveScreen;
  272.   x = s->MouseX;
  273.   y = s->MouseY;
  274.  
  275.   if (x < 1)
  276.     dx = s->Width * 2;
  277.   if (x >= s->Width - 1)
  278.     dx = (s->Width * -2);
  279.   if (y < 1)
  280.     dy = s->Height * 3;
  281.   if (y >= s->Height - 1)
  282.     dy = (s->Height * -3);
  283.  
  284.   inputReq->io_Command = IND_WRITEEVENT;
  285.   inputReq->io_Flags = 0;
  286.   inputReq->io_Length = sizeof(struct InputEvent);
  287.   inputReq->io_Data = (APTR)&phoney;
  288.  
  289.   phoney.ie_NextEvent = NULL;
  290.   phoney.ie_Class = IECLASS_RAWMOUSE;
  291.   phoney.ie_TimeStamp.tv_secs = 0;
  292.   phoney.ie_TimeStamp.tv_micro = 0;
  293.   phoney.ie_Code = IECODE_NOBUTTON;
  294.   phoney.ie_Qualifier = IEQUALIFIER_RELATIVEMOUSE;
  295.   phoney.ie_X = dx;
  296.   phoney.ie_Y = dy;
  297.  
  298.   DoIO(inputReq);
  299. }
  300.  
  301. Uninstall()
  302. {
  303.   if (!updating)
  304.     {
  305.     if (inputReq)
  306.       {
  307.       inputReq->io_Command = IND_REMHANDLER;
  308.       inputReq->io_Data = (APTR)&handlerStuff;
  309.       DoIO(inputReq);
  310.  
  311.       CloseDevice(inputReq);
  312.       DeleteStdIO(inputReq);
  313.       }
  314.  
  315.     if (inputPort)    DeletePort(inputPort);
  316.     if (signum > -1)  FreeSignal(signum);
  317.  
  318.     if (tdevice == 0)
  319.       {
  320.       AbortIO(&Timer_Req.tr_node);
  321.       CloseDevice(&Timer_Req);
  322.       }
  323.  
  324.     if (TimerPort)
  325.       DeletePort(TimerPort);
  326.  
  327.     if (defPortPtr)
  328.       {
  329.       if (defPortPtr->mp.mp_Node.ln_Name)
  330.         RemPort(defPortPtr);
  331.       FreeMem(defPortPtr,(long)sizeof(struct defPort));
  332.       }
  333.     }
  334.  
  335.   if (IntuitionBase)
  336.     CloseLibrary(IntuitionBase);
  337.   exit(0L);
  338. }
  339.